package cn.iocoder.yudao.module.trade.api.order;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
import cn.iocoder.yudao.module.trade.api.order.dto.TradeOrderItemBaseDTO;
import cn.iocoder.yudao.module.trade.api.order.dto.TradeOrderPageDTO;
import cn.iocoder.yudao.module.trade.api.order.dto.TradeOrderPageItemRespDTO;
import cn.iocoder.yudao.module.trade.api.order.dto.TradeOrderRespDTO;
import cn.iocoder.yudao.module.trade.convert.order.TradeOrderConvert;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderItemDO;
import cn.iocoder.yudao.module.trade.service.order.TradeOrderQueryService;
import cn.iocoder.yudao.module.trade.service.order.TradeOrderUpdateService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.*;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

/**
 * 订单 API 接口实现类
 *
 * @author HUIHUI
 */
@Service
@Validated
public class TradeOrderApiImpl implements TradeOrderApi {

    @Resource
    private TradeOrderUpdateService tradeOrderUpdateService;
    @Resource
    private TradeOrderQueryService tradeOrderQueryService;

    @Override
    public List<TradeOrderRespDTO> getOrderList(Collection<Long> ids) {
        return TradeOrderConvert.INSTANCE.convertList04(tradeOrderQueryService.getOrderList(ids));
    }

    @Override
    public TradeOrderRespDTO getOrder(Long id) {
        return TradeOrderConvert.INSTANCE.convert(tradeOrderQueryService.getOrder(id));
    }

    @Override
    public void cancelPaidOrder(Long userId, Long orderId) {
        tradeOrderUpdateService.cancelPaidOrder(userId, orderId);
    }

    @Override
    public Integer getOrderCountByUserGasId(Long id) {
        // 查询燃气户订单数量
        return tradeOrderQueryService.getOrderCountByUserGasId(id);
    }

    /**
     *  查询燃气户的订单
     * @param dto
     * @return
     */
    @Override
    public PageResult<TradeOrderPageItemRespDTO> selectTradeOrderByGasIdList(TradeOrderPageDTO dto) {
        PageResult<TradeOrderPageDTO> list = tradeOrderQueryService.getOrderListByUserGasId(dto);
        List<TradeOrderDO> tradeOrderDOS = BeanUtils.toBean(list.getList(), TradeOrderDO.class);
        if (CollectionUtil.isEmpty(tradeOrderDOS)){
            return new PageResult<>();
        }
        // 查询订单项
        List<TradeOrderItemDO> orderItems = tradeOrderQueryService.getOrderItemListByOrderId(
                convertSet(tradeOrderDOS, TradeOrderDO::getId));

        Map<Long, List<TradeOrderItemDO>> orderItemMap = convertMultiMap(orderItems, TradeOrderItemDO::getOrderId);

        // 转化 List
        List<TradeOrderPageItemRespDTO> orderVOs = CollectionUtils.convertList(tradeOrderDOS, order -> {
            List<TradeOrderItemDO> xOrderItems = orderItemMap.get(order.getId());
            TradeOrderPageItemRespDTO orderVO = BeanUtils.toBean(order, TradeOrderPageItemRespDTO.class).setItems(BeanUtils.toBean(xOrderItems, TradeOrderItemBaseDTO.class));
            // 处理收货地址
            orderVO.setReceiverAreaName(AreaUtils.format(order.getReceiverAreaId()));
            // 增加用户信息
            return orderVO;
        });
        return new PageResult<>(orderVOs, list.getTotal()) ;
    }

}
